package com.example.baiduai.control;

import com.example.baiduai.factory.ControllerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:处理所有servl请求的基类
 * @Time 2022/09/23 15:00
 */
@WebServlet({"/NlpHtml/baidu_ai", "/Face_Body/baidu_ai", "/AudioHtml/baidu_ai","/OcrHtml/baidu_ai"})
public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String model = req.getParameter("model");
        Controller controller = null;
        //System.out.println(model);

        switch (model) {
            case "nlp":
                controller = ControllerFactory.newInstance(NlpControl.class);
                break;
            case "face_body":
                controller = ControllerFactory.newInstance(FaceBodyControl.class);
                break;
            case "speech":
                controller = ControllerFactory.newInstance(SpeechControl.class);
                break;
            case "ocr":
                controller = ControllerFactory.newInstance(OcrControl.class);
                break;
            default:
                break;
        }
        //3,分发请求进行处理
        if (controller != null) {
            controller.handle(req, resp);
        }

    }
}
